def counting_sort(nums: list[int]):
    """计数排序"""  # 桶排序的变形
    # 找到最大值
    max_val = max(nums)

    # 初始化桶
    bucket = [0] * (max_val + 1)

    # 分配到桶中
    for num in nums:
        bucket[num] += 1  # 输入数据范围为非负整数

    # 遍历桶合并结果
    i = 0
    for num, count in enumerate(bucket):
        for _ in range(count):
            nums[i] = num
            i += 1


if __name__ == "__main__":
    # 设输入数据为浮点数，范围为 [0, 1)
    nums = [1, 0, 1, 2, 0, 4, 0, 2, 2, 4]
    counting_sort(nums)
    print("排序完成后 nums =", nums)
    # 排序完成后 nums = [0, 0, 0, 1, 1, 2, 2, 2, 4, 4]
